$(document).ready(function () {
    var kDataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: appBosch.read,
                dataType: "json"
            }
        },
        batch: true,
        schema: {
            model: {
                id: "value",
                fields: {
                    user: {type: "string"},
                    event: {type: "string"},
                    sDate: {type: "date"},
                    cant: {type: "number"}
                }
            }
        }
    });

    $("#grid").kendoGrid({
        toolbar: [
            {
                name: "pdf",
                text: "Exportar a PDF"
            },
            {
                name: "excel",
                text: "Exportar a Excel"
            }
        ],
        excel: {
            allPages: true,
            fileName: "Solicitudes de transporte.xlsx",
            filterable: true
        },
        pdf: {
            allPages: true,
            fileName: "Solicitudes de transporte.pdf"
        },
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        resizable: true,
        columnMenu: true,
        dataSource: kDataSource,
        columns: [
            {field: 'user', title: 'Solicitante'},
            {field: 'event', title: 'Curso'},
            {field: 'cant', title: 'Usuarios'},
            {
                field: 'sDate',
                title: 'Fecha',
                template: '<span title="#=kendo.toString(new Date(sDate), "F") #" >#=kendo.toString(new Date(sDate), "d") #</span>'
            },
            {
                command: [
                    {
                        name: "seeRequest",
                        text: "Ver solicitud",
                        click: seeRequest
                    }
                ]
            }
        ]
    });
});

function seeRequest(e) {
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var url = appBosch.urlTransporRequest + '/' + dataItem.id;
    var magicLink = $('#magicLink');
    magicLink.attr('href', url);
    magicLink.attr('target', '_blank');
    magicLink[0].click();
}